#include "thread.hpp"

using namespace com_oocl_monitor::system;


void Thread::run(){
	if (_sem != nullptr)
		WaitForSingleObject(*_sem, INFINITE);
    PM *pm=new PM(_run,_sem);
	HANDLE handle = (HANDLE)_beginthreadex(NULL, 0, threadFun, (LPVOID)pm, 0, NULL);
	CloseHandle(handle);
}
unsigned int __stdcall Thread::threadFun(LPVOID pM)
{
    PM *pm=static_cast<PM*>(pM);
    pm->_run->run();
    delete(pm->_run);
	if (pm->_sem != nullptr)
		ReleaseSemaphore(*(pm->_sem), 1, NULL);
    delete(pm);
	return 0;
}

